473,416 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,416 developers and data experts.

Position Absolute Center of Page

iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.     <meta http-equiv="Content-Language" content="en-us" />
  7.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8.     <title>DivCenter Experiment</title>
  9. </head>
  10.  
  11. <style>
  12. body, html
  13. {
  14.  width: 100%;
  15.  height: 100%;
  16.  margin: 0px; 
  17.  padding: 0px;
  18.  background-color: #FFFFFF;
  19. }
  20.  
  21. .favBgStyle
  22. {
  23.  border: 0px;
  24.  padding: 0px;
  25.  margin: 0px;
  26.  width: 100%;
  27.  position: absolute;
  28.  background-color: #000000;
  29.  opacity: 0.5;
  30.  filter: Alpha(opacity:50);
  31.  z-index: 0;
  32. }
  33.  
  34. .favAreaBg
  35. {
  36.  border: 0px;
  37.  padding: 0px;
  38.  position: absolute;
  39.  background-color: #FFFFFF;
  40.  z-index: 1;
  41. }
  42.  
  43. .occupySpace
  44. {
  45.  height: 1500px;
  46. }
  47. </style>
  48.  
  49. <script>
  50. function OffWindowH()
  51. {
  52.  var OffWindowH=0;
  53.  
  54.  window.scrollTo(0,10000000);
  55.  
  56.  if(typeof self.pageYOffset!='undefined')
  57.  OffWindowH=self.pageYOffset;
  58.  else if(document.compatMode && document.compatMode != 'BackCompat')
  59. OffWindowH=document.documentElement.scrollTop;
  60.  else if(document.body && typeof(document.body.scrollTop)!='undefined')
  61. OffWindowH=document.body.scrollTop;
  62.  window.scrollTo(0,0);
  63.  
  64.  return OffWindowH;
  65. }
  66.  
  67. function WindowHeight() 
  68. {
  69.   var WindowHeight = 0;
  70.   if( typeof( window.innerWidth ) == 'number' ) 
  71.   WindowHeight = window.innerHeight;
  72.   else if (document.documentElement &&  document.documentElement.clientHeight) 
  73.   WindowHeight = document.documentElement.clientHeight;
  74.   else if(document.body && document.body.clientHeight) 
  75.   WindowHeight = document.body.clientHeight;
  76.  
  77.   return WindowHeight;
  78. }
  79.  
  80. function pHeight()
  81. {
  82.   var pHeight = OffWindowH() + WindowHeight();
  83.   return pHeight;
  84. }
  85.  
  86. function favBg()
  87. {
  88.   var favBg = document.getElementById('favBg');
  89.   favBg.style.height = pHeight() + 'px';
  90. }
  91. var cdiv = "blank";
  92. function favArea() {
  93.   if (cdiv == "blank") { cdiv = window.setInterval("favArea()", 50); }
  94.  var Element = document.getElementById("favArea");
  95.  objh = parseFloat(Element.style.height)/2;
  96.  objw = parseFloat(Element.style.width)/2;
  97.  Element.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
  98.  Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';
  99. }
  100. </script>
  101.  
  102. <body onload="favBg(); favArea();">
  103. <div id="favArea" class="favAreaBg"  style="width: 500px; height: 400px;"><table width="100%" height="100%"><tr><td valign="middle" align="center">This is dead center</td></tr></table></div>
  104. <div id="favBg" class="favBgStyle"></div>
  105. <div class="occupySpace"></div>
  106. </body>
  107. </html>
  108.  

This is another example written by me that will put a div dead center of the window.
Jul 17 '07 #1
5 19635
Frinavale
9,735 Expert Mod 8TB
Where are you using the OffWindowH() and the WindowHeight() methods?
You've declared them but I don't actually see them being used.
Feb 20 '09 #2
Dormilich
8,658 Expert Mod 8TB
they are used in pHeight(), line #82.
Feb 21 '09 #3
Frinavale
9,735 Expert Mod 8TB
Oh, I see, thanks :)
Feb 22 '09 #4
hsriat
1,654 Expert 1GB
But you can use the CSS alternative too.
Expand|Select|Wrap|Line Numbers
  1. body {
  2.     margin: 0;
  3. }
  4. #center_div {
  5.     position: fixed;
  6.     height: Hpx;
  7.     width: Wpx;
  8.     top: 50%;
  9.     left: 50%;
  10.     margin: -H/2px -W/2px;
  11. }
Apr 10 '09 #5
Hi hsriat,
Thanks for the CSS, it worked perfectly for what I needed.
May 7 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Nige | last post by:
I've just given one of my sites (www.wealdbroadband.co.uk) an overhaul. Anyone know if there is a way of fixing text at a certain relative position down a page? I'd like the "(c) 2003 ..." notice...
0
by: Mike | last post by:
I've been working on a pop-up menu, one of those where you mouse over a button and a submenu pops up. Because the client wanted the menu centered, I gave the starting point of the submenu an...
3
by: Alexander Fischer | last post by:
Hello everybody, can someone help me with this problem: I'm creating a page with a sidebar, and I wanted to create the sidebar as a div which gets a "position:absolute". The problem: if the...
2
by: Joachim Bauer | last post by:
I'm using the code below to display a menu that opens when the mouse goes over the main menu item (try it in your browser to understand the behaviour). It uses "position:absolute" and a switch...
1
by: jen_designs | last post by:
Is there a way to position a div to absolute top but also center in the browser?
2
by: Laphan | last post by:
Hi All I have developed a site whereby it uses the contents of a folder (incs gifs, jpgs and css files) to display the web 'skin' of the site. This is OK, but each button on the site is an...
19
by: derelicten | last post by:
hello , I have an issue positionating some pics I want to anchor to an existing table cell but I cant just place regular position on the cell because the background is fixed height and the set of...
6
by: ThunderMusic | last post by:
Hi, I think the subject says it all... thanks ThunderMusic
1
by: jobs | last post by:
I have a simple login page with roughly a 300px x 300px image behind the asp.net login control. I need the login control to land in the middle of the image. I want both the image and the login...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.